import Vue from 'vue'
import App from './App.vue'
import router from './router'
//  引入store
import store from './store'
import './plugins/element.js'
// 导入全局样式
import './assets/css/global.css'

import axios from 'axios'

import utils from './utils'

// 配置请求根路径
axios.defaults.baseURL = 'http://127.0.0.1/api/'
//  设置请求头
axios.interceptors.request.use(config => {
  config.headers.token = window.sessionStorage.getItem('setToken')
  return config
})
// 将axios挂载到vue原始对象
Vue.prototype.$http = axios

Vue.prototype.utils = utils

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App),
  store
}).$mount('#app')

router.beforeEach((to, from, next) => {
  //  判断当前访问的路由是否需要登录权限
  if (to.matched.some(record => record.meta.requireAuth)) {
    //  判断当前用户登录信息是否存在
    if (sessionStorage.getItem('setToken')) {
      next()
    } else {
      next({
        path: '/'
      })
    }
  } else {
    next()
  }
})
